package com.example.spring_config.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.spring_config.mapper.Permission;
import com.example.spring_config.mapper.User;
import com.example.spring_config.service.PermissionService;
import com.example.spring_config.service.UserService;
import com.example.spring_config.util.FormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Set;

@Controller
public class PageController {

    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;

    /**
     * 跳转登录页面
     * @param session
     * @param id
     * @return
     */
    @RequestMapping("/index")
    public String toIndex(HttpSession session,String id,Model model){
        User attribute = (User) session.getAttribute("login_user");
        if(attribute==null){
            return "login";
        }else {
            Set<Permission> userPermission = permissionService.getUserPermission(attribute.getId());
            List<Permission> permissions = FormatUtil.formatData(userPermission);
            model.addAttribute("permissionList",permissions);
            model.addAttribute("userid",attribute.getId());
            return "index";
        }
    }

}
